<style scoped>
.layui-form>>>.layui-form-select {
    z-index: inherit;
}
</style>

<template>
<div class="company">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>修改库间</legend>
    </fieldset>

    <form class="layui-form ">
        <input type="hidden" name="guid" :value="guid" />
        <div class="layui-form-item">
            <label class="layui-form-label">库房名称:</label>
            <div class="layui-input-block">
                <input type="hidden" name="storeguid" :value="storeroom != '' && storeroom != undefined ? storeroom.guid : storehouseroom.storeguid" />
                <select class="layui-select" lay-filter="loadStorehouseroom1" id="storeguid">
                    <option v-for="(emnu, i) in storerooms"  :value="emnu" :selected="emnu.guid == storehouseroom.storeguid">{{emnu.name}}</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">库间类型:</label>
            <div class="layui-input-block">
                <select class="layui-select" lay-verify="required" id="shrtype" ref="shrtype" lay-filter="type" name="shrtype">
                    <option value="">请选择</option>
                    <option   v-for="(item, i) in types" :value="item.dictkey" :selected="item.dictkey == storehouseroom.shrtype">{{item.dicttext}}</option>
                </select>

            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">库间编码: </label>
            <div class="layui-input-block" style="width: 300px">
                <select class="layui-select" id="code1" name="code1" lay-filter="code1">
                    <option v-for="(rooms, i) in storehouserooms" :value="rooms.code"> {{rooms.name}} ({{rooms.code}}) </option>
                </select>
                <input type="number" id="code2" lay-filter="code2" name="code2" required lay-verify="required" style="width: 200px; position: absolute;left: 300px;top: 0px;" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">库间名称:</label>
            <div class="layui-input-block">
                <input type="text" name="name" :value="storehouseroom.name" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">管理人员:</label>
            <div class="layui-input-block">
                <input type="text" name="HandoverCompany" placeholder="请选择管理人员" value="" id="HandoverCompany" class="layui-input" style="position:absolute;width:80%;" onkeyup="search()" autocomplete="off">
                <select type="text" name="managerid" id="hc_select" lay-filter="hc_select" autocomplete="off" class="layui-select" lay-search>

                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">库间容量(吨)</label>
            <div class="layui-input-inline">
                <input type="text" name="capacity" :value="storehouseroom.capacity" placeholder="" autocomplete="off" class="layui-input">

            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">备注:</label>
            <div class="layui-input-block">
                <input type="text" name="remark" :value="storehouseroom.remark" placeholder="" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="submit">立即提交</button>
                <button class="layui-btn layui-btn-primary" @click="$parent.back">返回</button>
            </div>
        </div>
    </form>
</div>
</template>

<script>
export default {
    name: 'AddStorehouseroom',
    props: ['id'],
    data() {
        return {
            houses: [],
            storerooms: [],
            storeroom: '',
            storehouserooms: [],
            storehouseroom: {},
            guid: this.id,
            managerids: [],
            types: []
        }
    },
    mounted() {
        let that = this;
        var form = layui.form;
        that.getBaseinfo();
        that.loadSelect();
        that.loadRooms();
        that.loadUser();
        that.loadEmnu();

        form.render();
        layui.use(['form'], function () {
            var form = layui.form,
                layer = layui.layer;
            form.render();

            form.on('submit(submit)', function (fdata) {

                let loader = layer.load();

                let postdata = fdata.field;
                console.log('postdata:', fdata.field)
                // return false;
                $.ajax({
                    url: '/repository/Storehouseroom!updateVUEPC.action',
                    data: fdata.field,
                    type: "post",
                    dataType: "json",
                    success: function (res) {
                        if (res.info == "success") {
                            that.toast("修改成功");
                            that.$parent.switchPage('Storehouseroom');
                        } else {
                            that.toast(res.errorMsg);
                        }
                    },
                    complete: function () {
                        layer.close(loader);
                    }
                });
                return false;
            });

            form.on('select(loadStorehouseroom1)', function (data) {
                that.storeroom = data.value;
                console.log('storeroom1:', that.storeroom)
                that.loadHouses(data.value.guid);
                layui.form.render();
            });

            form.on('select(type)', function (data) {
                let status = that.chaneType(that.storehouseroom.storeguid, that.storehouseroom.guid);
                if (!status) {
                    that.toast("该库间库存不为空，不能修改库间类型！");
                    $('#shrtype').val(2);
                    layui.form.render();

                }
            });
        });
        //监听输入编码校验
        $('#code2').change(function () {
            if (!that.delcheckSub(that.storehouseroom.code)) {
                return false;
            }
            if (!that.check()) {
                return;
            }
            if (!that.checkRE($(this).val())) {
                return false;
            }
        })
    },
    updated() {
        layui.form.render();
    },
    methods: {
        //加载用户权限信息
        loadUser: function () {
            var that = this;
            $.ajax({
                url: '/repository/Storehouseroom!getManager.action',
                type: "post",
                dataType: "json",
                success: function (res) {
                    if (res != undefined && res.info != undefined) {
                        var data = res.info;
                        $.each(data, function (index, item) {
                            if (item.guid == that.storehouseroom.managerid) {

                                $('#hc_select').append("<option selected =true value=" + item.guid + " > " + item.realname + "(" + item.telphone + ")" + "</option>"); // 下拉菜单里添加元素
                            } else {
                                $('#hc_select').append("<option   value=" + item.guid + " > " + item.realname + "(" + item.telphone + ")" + "</option>"); // 下拉菜单里添加元素

                            }
                        });
                        layui.form.render("select");

                    } else {
                        alert("系统错误");
                    }
                }
            });
        },
        getBaseinfo: function () {
            var that = this;
            $.ajax({
                url: '/repository/Storehouseroom!get.action',
                type: "post",
                dataType: "json",
                data: {
                    guid: that.guid
                },
                success: function (res) {
                    that.storehouseroom = res.info;
                    if (that.storerooms.length != 0 && that.storeroom == '') {
                        that.storeroom = that.storerooms.filter(item => {
                            return item.guid == that.storehouseroom.storeguid;
                        })[0];
                        console.log('storeroom2:', that.storeroom)

                    }
                    that.loadHouses(that.storehouseroom.storeguid);

                }
            });
        },
        loadRooms: function () {
            var that = this;
            $.ajax({
                url: '/repository/Storeroom!getStoreroomJson.action',
                type: "post",
                dataType: "json",
                success: function (res) {
                    if (res.info != undefined) {
                        that.storerooms = res.info;

                        if (that.storehouseroom != '' && that.storeroom == '') {
                            that.storeroom = that.storerooms.filter(item => {
                                return item.guid == that.storehouseroom.storeguid;
                            })[0];
                            console.log('storeroom3:', that.storeroom)
                        }
                    }
                    // $.each(res.info, function (index, item) {
                    //
                    //     if (item.guid == that.storehouseroom.storeguid) {
                    //         $('#storeguid').append("<option  selected =true  value=" + item.guid + " > " + item.name + "</option>"); // 下拉菜单里添加元素
                    //
                    //     } else {
                    //         $('#storeguid').append("<option   value=" + item.guid + " > " + item.name + "</option>"); // 下拉菜单里添加元素
                    //
                    //     }
                    // });
                    layui.form.render("select");

                }
            });
        },
        loadSelect: function () {
            layui.use(['form', 'layedit', 'upload'], function () {
                var form = layui.form
                form.on('select(hc_select)', function (data) { //选择移交单位 赋值给input框
                    $("#HandoverCompany").val(data.value);
                    $("#hc_select").next().find("dl").css({
                        "display": "none"
                    });
                    form.render();

                });

                window.search = function () {
                    var value = $("#HandoverCompany").val();
                    $("#hc_select").val(value);

                    form.render();
                    $("#hc_select").next().find("dl").css({
                        "display": "block"
                    });
                    var dl = $("#hc_select").next().find("dl").children();
                    var j = -1;
                    for (var i = 0; i < dl.length; i++) {
                        if (dl[i].innerHTML.indexOf(value) <= -1) {
                            dl[i].style.display = "none";
                            j++;
                        }
                        if (j == dl.length - 1) {
                            $("#hc_select").next().find("dl").css({
                                "display": "none"
                            });
                        }
                    }

                }
            });
        },
        loadHouses(data) {
            var that = this;
            $.ajax({
                url: '/repository/Storehouseroom!getAll.action',
                type: "post",
                dataType: "json",
                data: {
                    storeguid: data
                },
                success: function (res) {
                    let roomcode = that.storeroom != '' && that.storeroom != undefined ? that.storeroom.code : that.storehouseroom.storecode;

                    $('#code1').empty();
                    $('#code1').append("<option value='" + roomcode + "'>一级库房(" + roomcode + ")</option>");
                    $.each(res.info, function (index, item) {
                        var srhcode = that.storehouseroom.code;
                        console.log('srhcode:', srhcode)
                        var srhcode1 = srhcode.substring(0, srhcode.length - 3);
                        // if (item.code == srhcode1 && srhcode.length != 3) {
                            var srhcode2 = srhcode.substring(srhcode.length - 3, srhcode.length);
                            console.log('srhcode2:', srhcode2)

                            $('#code2').val(srhcode2);
                            $('#code1').append("<option  selected='true' value=" + srhcode1 + " > " + item.name + "(" + srhcode1 + ")" + "</option>"); // 下拉菜单里添加元素
                        // } else {
                        //     if ($('#code2').val() == '' || $('#code2').val() == null) {
                        //         $('#code2').val(srhcode);
                        //     }
                        //     $('#code1').append("<option   value=" + item.code + " > " + item.name + "(" + item.code + ")" + "</option>"); // 下拉菜单里添加元素
                        // }
                    });
                    layui.form.render("select");
                }
            });
        },

        chaneType(srhguid, id) {
            let that = this
            let params = {
                "srhguid": srhguid,
                "id": id
            }
            let status = false;
            this.axios({
                methods: "post",
                url: '/harvest/Whinfo!checkWhin.action',
                params
            }).then(result => {
                if (result.data.msg == "success") {
                    status = result.data.result == 'true' ? true : false;
                } else {
                    that.alert("系统错误")
                }

            })
            return status;
        },
        check(data) {
            var code2 = $('#code2').val();
            var code1 = $('#code1').val();
            var code = code1 + code2;
            if (code2.length % 3 != 0) {
                this.toast("编码长度必须为  “3”的倍数, 请重新填写！");
                $('#code2').val(this.storehouseroom.code);
                return false;
            }
            return true;
        },
        checkRE(codeVal) {
            console.log(codeVal)
            var that = this;
            var checkResult = true;
            $.ajax({
                url: '/repository/Storehouseroom!list.action',
                type: "post",
                dataType: "json",
                data: {
                    code: codeVal
                },
                success: function (res) {
                    if ((res.rows.length > 1)) {
                        // that.toast("输入库间编码已存在，请重新输入！");
                        layer.alert("输入库间编码已存在，请重新输入！")
                        $('#code2').val(that.storehouseroom.code);
                        checkResult = false;
                    }
                }
            });
            return checkResult;
        },
        delcheckSub(code) {
            var that = this;
            var checkResult = true;
            $.ajaxSettings.async = false;
            $.post('/repository/Storehouseroom!list.action', {
                housecode: that.storehouseroom.code
            }, function (result) {
                if (result.rows.length != 1) {
                    that.toast("此库间存在下级库间，不能修改编码！");
                    $('#code2').val(that.storehouseroom.code);
                    checkResult = false;
                }
            }, 'json');
            return checkResult;
        },

        loadEmnu: function () {
            var that = this;
            $.ajax({
                url: '/sales/Sellform!getDictEmnu.action',
                type: "post",
                dataType: "json",
                data: {
                    dicttype: "22"
                },
                success: function (res) {
                    if (res.info != undefined) {
                        that.types = res.info;
                    }
                    // $.each(res.info, function (index, item) {
                    //     if (item.dictkey == that.storehouseroom.shrtype) {
                    //
                    //         $('#shrtype').append("<option   selected =true  value=" + item.dictkey + " > " + item.dicttext + "</option>"); // 下拉菜单里添加元素
                    //     } else {
                    //         $('#shrtype').append("<option   value=" + item.dictkey + " > " + item.dicttext + "</option>"); // 下拉菜单里添加元素
                    //     }
                    // });
                }
            });
        },
    }
}
</script>
